<?php
namespace app\api\controller;
use app\common\model\Setup as SetupModel;
use app\common\model\ProjectClassify as ProjectClassifyModel;
use app\common\model\Project as ProjectModel;

use think\Controller;
use think\Config;
use think\Db;
use think\Session;
use think\Cookie;

/**
 * 扫码核销
 * Class Project
 * @package app\api\controller
 */
class Project extends Controller
{
    protected $setup_model;
    protected $project_classify_model;
    protected $project_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->setup_model                   = new SetupModel();
        $this->project_classify_model        = new ProjectClassifyModel();
        $this->project_model                 = new ProjectModel();

        $setup   = $this->setup_model->find(1);

    }

    /**
     * 手机验证码
     */
    public function index()
    {
        $setup   = $this->setup_model->find(1);

        $project_classify  = $this->project_classify_model->where(["pid"=>0,"status"=>1])->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
        $project_classify_list=[];
        foreach($project_classify as $key => $value) {
            //初始化
            $value_date=[];

            $project_classify_level  = $this->project_classify_model->where(["pid"=>$value['id'],"status"=>1])->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
            $project_classify_level_list=[];

            //初始化时间
            $time="";
            foreach($project_classify_level as $kk => $vv) {
                //初始化
                $vv_date=[];

                //记录时间
                $project_time   = $this->project_model->where(["cid"=>$vv['id'],"status"=>1])->order(['status' => 'DESC', 'score' => 'DESC', 'id' => 'DESC'])->find();
                $time = date('Y-m-d',strtotime($project_time['create_time']));

                $project = $this->project_model->where(["cid"=>$vv['id'],"status"=>1])->order(['status' => 'DESC', 'score' => 'DESC', 'id' => 'DESC'])->select();
                $project_count  = $this->project_model->where(["cid"=>$vv['id'],"status"=>1])->count();
                $vv_date['list']=$project;
                $vv_date['count']=$project_count;

                $project_classify_level_list[]=$vv_date;
            }


            $value_date['title']=$value['title'];
            $value_date['time']=$time;
            $value_date['list']=$project_classify_level_list;
            $project_classify_list[]=$value_date;
        }


        if ($project_classify_list) {
            $result = [
                'status'  => "ok",
                'title' => $setup['title'],
                'message' => $project_classify_list
            ];
        }else {
            $result = [
                'status'  => "fail",
                'title' => $setup['title'],
                'message' => "数据不存在"
            ];
        }

        return json($result);
    }

}